package com.lagou;

import com.alibaba.fastjson.JSONObject;
import com.lagou.server.ZkServer;
import com.lagou.service.UserServiceImpl;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import com.lagou.service.UserServiceImpl;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
public class ServerBootstrap {

    public static void main(String[] args) throws Exception, InterruptedException {

        SpringApplication.run(ServerBootstrap.class, args);
        String  path="/Server";//永久跟节点
        String ip="127.0.0.1";
        int port=8990;
        //1：启动服务
        UserServiceImpl.startServer(ip,port);

        ZkClient zkClient = new ZkClient("192.168.115.129:2181");

        /**
         *  2：启动就向zk注册服务
         *  创建临时节点
         */
        JSONObject json=new JSONObject();
        json.put("key",ip+port);
        json.put("ip",ip);
        json.put("port",port);
        json.put("time",new Date().getTime());
        json.put("consume",0);
        zkClient.createEphemeral(path+"/"+ip+":"+port,json);//创建临时节点，断开连接自动 回收服务
        System.out.println("zk服务注册成功！");





    }








}
